<?php
// +----------------------------------------------------------------------
// | CLTPHP.Pro [会员管理]
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2029 https://www.cltphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://www.cltphp.com/detail-140.html )
// +----------------------------------------------------------------------
// | Author: chichu <1109305987@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\model\UserLevel;
use app\common\model\Users as M;
use think\facade\Request;
use think\facade\Db;
use think\facade\View;

class Users extends Base
{
    public function recharge()
    {
        $id = input('id');
        $info = \app\common\model\Users::edit($id);
        if(\request()->isPost()){
            $number = input('number');
            if($number>0){
                $is_add = 1;
                $log_type = 1;
            }else{
                $is_add = 2;
                $log_type = 2;
            }
            try {
                \app\common\model\Wallet::addDelMoney($id, $number, $is_add, $log_type, '后台操作');
                return $this->ajaxSuccess();
            }catch (\Exception $e){
                return $this->ajaxError($e->getMessage());
            }
        }else{
            return \view('', compact('info'));
        }
    }

    //会员列表
    public function index()
    {
        if (Request::isAjax()) {
            //分页
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $page = ['list_rows' => $pageSize, 'page' => $page];

            $key = input('post.key');
            $where = [];
            if (!empty($key)) {
                $where[] = ['mobile|username', 'like', '%' . $key . '%'];
            }
            //排序
            $sort_order = input('sort_order') ? input('sort_order') : 'desc';
            $order = input('sort_order') ? [input('sort_by') => $sort_order] : ['create_time' => $sort_order];

            //print_r($where);
            $list = M::getList($where, $page, $order)->toArray();

            return ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        } else {
            return view();
        }
    }

    //字段值修改
    public function cf()
    {
        $data = input('post.');
        $result = M::editSave($data);
        return $result;
    }

    public function edit()
    {
        if (request()->isPost()) {
            $data = input('post.');

            if (empty($data['password'])) {
                unset($data['password']);
            } else {
                $token = M::where('id', $data['id'])->value('token');
                $data['password'] = md5($token . $data['password']);
            }
            $result = M::editSave($data);
            return $result;
        } else {
            $id = input('param.id');
            if (empty($id)) {
                return ['code' => 1, 'msg' => '缺少必要的参数！'];
            }
            $info = M::where('id', $id)->find();

            $user_level = UserLevel::order('sort asc')->select();

            $province = Db::name('Region')->where(array('pid' => 1))->select();
            $city = Db::name('Region')->where(array('pid' => $info['province']))->select();
            $area = Db::name('Region')->where(array('pid' => $info['city']))->select();
            $view = [
                'info' => $info,
                'user_level' => $user_level,
                'province' => $province,
                'city' => $city,
                'area' => $area
            ];
            return view('form', $view);
        }
    }

    //删除会员
    function del()
    {
        $id = input('param.id');
        return M::del($id);
    }

    //批量删除
    public function delall()
    {
        $ids = input('post.ids');
        M::del($ids);
        return ['code' => 0, 'msg' => '删除成功！'];
    }

    //地区联动
    public function getRegion()
    {
        $pid = input("pid");
        if (!$pid) {
            return '';
        }
        $list = Db::name("region")->where('pid', $pid)->select()->toArray();
        return json($list);
    }
}